package cn.net.autocode.platform.providers.rules;

import cn.hutool.core.util.StrUtil;
import cn.net.autocode.platform.constants.PlatformConstant;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import cn.net.autocode.dbManager.handler.PreparedStatementHandler;
import cn.net.autocode.dbManager.SqlWrapperBuilder;

public class QueryRules {


    public static void preHandle(JSONArray rules, JSONObject param, SqlWrapperBuilder swb, String tableAlis){
        int size = rules.size();
        for(int i=0;i<size;i++){
            JSONObject rule = rules.getJSONObject(i);
            if(rule.getIntValue("ruleType")==32){//模糊匹配
                ruleLike(rule, param, swb,tableAlis);
            }

            if(rule.getIntValue("ruleType")==31){//默认条件
                ruleDefault(rule, param, swb,tableAlis);
            }
        }
    }

    /**
     * 模糊匹配
     */
    private static void ruleLike(JSONObject rule, JSONObject param, SqlWrapperBuilder swb,String tableAlis){

        String fieldName = rule.getString("fieldName");
        String refColumn = rule.getString("refColumn");

        String value = null;
        if(param.containsKey(fieldName)){
            value = param.getString(fieldName);
        }
        if(value==null|| value.isBlank()){
            return;
        }else{
            value=value.trim();
        }

        if(StrUtil.isBlank(refColumn)){
            return;
        }

        param.remove(fieldName);

        JSONArray columns = JSONArray.parseArray(refColumn);

        if(columns.size()==1){
            swb.like(columns.getString(0),value);
        }else if(columns.size()>1){
            int columnSize=columns.size();
            String where1 = "";
            Object[] where2 = new Object[columnSize];
            for(int i=0;i<columnSize;i++){
                if(StrUtil.isBlank(tableAlis)){
                    where1 = where1+ " or " +columns.getString(i)+" like ? ";
                }else{
                    where1 = where1+ " or " +tableAlis+"."+columns.getString(i)+" like ? ";
                }

                where2[i] = "%"+value+"%";
            }
            if(StrUtil.isNotBlank(where1)){
                where1 = "("+where1.substring(4)+")";
                swb.where(where1,where2);
            }
        }

    }


    /**
     * 默认条件
     */
    private static void ruleDefault(JSONObject rule, JSONObject param, SqlWrapperBuilder swb,String tableAlis){
        String columnName = rule.getString("fieldName");
        String defaultValue = rule.getString("defaultValue");
        String fieldName = PreparedStatementHandler.columnToProperty(columnName);

        //设置默认值
        if(!param.containsKey(fieldName)||param.get(fieldName)==null||StrUtil.isBlank(param.getString(fieldName))){
            if(defaultValue.startsWith("@")){
                String lowFlag = defaultValue.trim().toLowerCase();
                if("@now".equals(lowFlag)){
                    if(StrUtil.isNotBlank(tableAlis)){
                        swb.eq(tableAlis+"."+columnName, PlatformConstant.dateValue(defaultValue));
                    }else{
                        swb.eq(columnName, PlatformConstant.dateValue(defaultValue));
                    }
                }else{
                    if(StrUtil.isNotBlank(tableAlis)){
                        swb.eq(tableAlis+"."+columnName, PlatformConstant.strValue(defaultValue));
                    }else{
                        swb.eq(columnName, PlatformConstant.strValue(defaultValue));
                    }
                }
            }else{
                if(StrUtil.isNotBlank(tableAlis)){
                    swb.eq(tableAlis+"."+columnName, defaultValue);
                }else{
                    swb.eq(columnName, defaultValue);
                }
                param.remove(fieldName);
            }

        }

    }



}
